/* components */
import BaseErrorPageNoPermission from '@src/component/common/BaseErrorPage/BaseErrorPageNoPermission.tsx'
/* enum */
import ComponentNameEnum from '@model/enum/ComponentNameEnum'
/* vue */
import VC from '@model/VC'
import { Component, Emit, Prop, Ref } from 'vue-property-decorator'
import { CreateElement, VNode } from 'vue'

@Component({ 
  name: ComponentNameEnum.Permission,
  components: { BaseErrorPageNoPermission }
})
export default class Permission extends VC {
  /* 是否有权限 */
  @Prop() readonly auth: boolean | undefined
  /* 显示的提示文字 */
  @Prop() readonly text: string | undefined
  
  /**
   * @description: 获取渲染的的内容
   * @return {VNode | VNode[] | undefined | null} 渲染的内容
   */
  private getRenderContent(): VNode | VNode[] | undefined | null {
    // 没权限且有提示文字则显示错误无权限页面
    if (this.auth === false && this.text) {
      return <base-error-page-no-permission text={this.text} />
    }
    
    return this.$slots.default
  }
  
  render(h: CreateElement): VNode | null {
    // 没权限且没提示文字则返回空
    if (!this.auth && !this.text) return null
    
    return (
      <div class={ComponentNameEnum.Permission}>
        { this.getRenderContent() }
      </div>
    )
  }
}

